package com.example.jdbcdemo.service.Impl;

import com.example.jdbcdemo.mapper.GoodsMapper;
import com.example.jdbcdemo.mapper.ProductMapper;
import com.example.jdbcdemo.mapper.StorageMapper;
import com.example.jdbcdemo.pojo.Goods;
import com.example.jdbcdemo.pojo.Product;
import com.example.jdbcdemo.pojo.Storage;
import com.example.jdbcdemo.service.ProductServiceInterface;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

@Service
public class ProductServiceImpl implements ProductServiceInterface {

    @Autowired
    ProductMapper productMapper;
    @Autowired
    GoodsMapper goodsMapper;
    @Autowired
    StorageMapper storageMapper;

    @Override
    public String getProductNameById(int id) {
        Product product= productMapper.queryProductById(id);
        return product.getName();
    }

    @Override
    public Map<String, Object> queryProductById(int id) {
        Map<String,Object> resultMap=new LinkedHashMap<>();
        Product product= productMapper.queryProductById(id);
        resultMap.put("productId",id);
        resultMap.put("name",product.getName());
        resultMap.put("wholeSale",product.getWholeSale());
        resultMap.put("singleSale",product.getSingleSale());
        resultMap.put("cost",product.getCost());
        List<Goods> goodsList=goodsMapper.queryGoodsListByProductId(id);
        List<Map<String,Object>> storageList=new LinkedList<>();
        for (Goods goods:goodsList)
        {
            Map<String,Object> storageMap=new LinkedHashMap<>();
            storageMap.put("id",goods.getStorageId());
            Storage storage= storageMapper.queryStorageById(goods.getStorageId());
            storageMap.put("name",storage.getAddress());
            storageMap.put("number",goods.getNumber());
            storageList.add(storageMap);
        }
        resultMap.put("storage",storageList);
        return resultMap;
    }
}
